package com.wangwei.modules.utils

import com.wangwei.modules.utils.scan.StringUtil
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.BufferedInputStream

class StreamUtil {

	companion object {


		/**
		 * 获取输入
		 *
		 * @param in
		 * @return
		 * @throws IOException
		 */
		fun inputStream2String3(inputStream: InputStream): String {
			var out: StringBuffer = StringBuffer()
			var br: BufferedReader = BufferedReader(InputStreamReader(inputStream));

			var length = 0;
			while (true) {
				var line = br.readLine()
				if (line != null)
					out.append(line + "\n")
				if (line.contains("Content-Length")) {
					length = Integer.parseInt(line.split(":")[1].trim())
				}
				if ("".equals(line.trim())) {
					break
				}

			}
			return out.toString()
		}

		fun inputStream2String(inputStream: InputStream?): String {
			var out: StringBuffer = StringBuffer()
			var br = BufferedInputStream(inputStream);
			var count = br.available()
			if (count == 0)
				count = 8192
			var b = ByteArray(count)
			br.read(b)
			out.append(String(b))

			return out.toString()
		}

		fun inputStream2String11(inputStream: InputStream): String {
			var out: StringBuffer = StringBuffer()
			var br: BufferedReader = BufferedReader(InputStreamReader(inputStream));
			while (true) {
				if (br.ready()) {
					var i = br.read();
					if (i != -1) {
						var aa = StringUtil.num2Char(i)
						out.append(aa)
					} else {
						break
					}
				} else {
					break
				}
			}
			return out.toString()
		}


		fun String2Byte(str: String) = java.lang.String(str).getBytes()


	}
}